package com.open.capacity.prints.service.impl;

import com.open.capacity.prints.util.ChineseYuanUtil;
import com.open.capacity.prints.util.uuid.UUIDUtils;
import com.open.capacity.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.prints.entity.BillGhzp;
import com.open.capacity.prints.dao.BillGhzpDao;
import com.open.capacity.prints.service.BillGhzpService;


@Service
public class BillGhzpServiceImpl implements BillGhzpService {

    @Autowired
    private BillGhzpDao billGhzpDao;

    /**
     * 添加
     *
     * @param billGhzp
     */
    public int save(BillGhzp billGhzp) {
        billGhzp.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        billGhzp.setCreateTime(new Date());

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.parseDate2(billGhzp.getDjrq().replaceAll("-", "").replaceAll(" ", "")));                    //放入Date类型数据
        billGhzp.setYear(calendar.get(Calendar.YEAR));                    //获取年份
        billGhzp.setMonth(String.valueOf(calendar.get(Calendar.MONTH)));                    //获取月份
        billGhzp.setDay(String.valueOf(calendar.get(Calendar.DATE)));                    //获取日
        billGhzp.setMoney1(ChineseYuanUtil.number2CNMontrayUnit(billGhzp.getMoney2()));

        return billGhzpDao.save(billGhzp);
    }

    /**
     * 修改
     *
     * @param billGhzp
     */
    public int update(BillGhzp billGhzp) {
        billGhzp.setCreateTime(new Date());

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.parseDate2(billGhzp.getDjrq().replaceAll("-", "").replaceAll(" ", "")));                    //放入Date类型数据
        billGhzp.setYear(calendar.get(Calendar.YEAR));                    //获取年份
        billGhzp.setMonth(String.valueOf(calendar.get(Calendar.MONTH)));                    //获取月份
        billGhzp.setDay(String.valueOf(calendar.get(Calendar.DATE)));                    //获取日
        billGhzp.setMoney1(ChineseYuanUtil.number2CNMontrayUnit(billGhzp.getMoney2()));

        return billGhzpDao.update(billGhzp);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return billGhzpDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public BillGhzp findById(String id) {
        BillGhzp item = billGhzpDao.findById(id);
        BigDecimal resut = item.getMoney2();
        Integer million5Count = resut.divide(BigDecimal.valueOf(100000000)).intValue();
        item.setMillion5(million5Count);
        resut = resut.subtract(BigDecimal.valueOf(million5Count * 100000000));

        Integer million4Count = resut.divide(BigDecimal.valueOf(10000000)).intValue();
        item.setMillion4(million4Count);
        resut = resut.subtract(BigDecimal.valueOf(million4Count * 10000000));


        Integer million3Count = resut.divide(BigDecimal.valueOf(1000000)).intValue();
        item.setMillion3(million3Count);
        resut = resut.subtract(BigDecimal.valueOf(million3Count * 1000000));

        Integer million2Count = resut.divide(BigDecimal.valueOf(100000)).intValue();
        item.setMillion2(million2Count);
        resut = resut.subtract(BigDecimal.valueOf(million2Count * 100000));

        Integer million1Count = resut.divide(BigDecimal.valueOf(10000)).intValue();
        item.setMillion1(million1Count);
        resut = resut.subtract(BigDecimal.valueOf(million1Count * 10000));

        Integer thousandCount = resut.divide(BigDecimal.valueOf(1000)).intValue();
        item.setThousand(thousandCount);
        resut = resut.subtract(BigDecimal.valueOf(thousandCount * 1000));

        Integer hundredCount = resut.divide(BigDecimal.valueOf(100)).intValue();
        item.setHundred(hundredCount);
        resut = resut.subtract(BigDecimal.valueOf(hundredCount * 100));

        Integer tenCount = resut.divide(BigDecimal.valueOf(10)).intValue();
        item.setTen(tenCount);
        resut = resut.subtract(BigDecimal.valueOf(tenCount * 10));

        Integer yuanCount = resut.divide(BigDecimal.valueOf(1)).intValue();
        item.setYuan(yuanCount);
        resut = resut.subtract(BigDecimal.valueOf(yuanCount * 1));

        Integer jiaoCount = resut.divide(BigDecimal.valueOf(0.1)).intValue();
        item.setJiao(jiaoCount);
        resut = resut.subtract(BigDecimal.valueOf(jiaoCount * 0.1));

        Integer fenCount = resut.divide(BigDecimal.valueOf(0.01)).intValue();
        item.setFen(fenCount);
        return item;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<BillGhzp> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<BillGhzp> list = billGhzpDao.findAll(params);
        PageInfo<BillGhzp> pageInfo = new PageInfo(list);

        return PageResult.<BillGhzp>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<BillGhzp> findAll(Map<String, Object> params) {
        return billGhzpDao.findAll(params);
    }

}
